<?php

	namespace App\Http\Controllers;

	use Illuminate\Http\Request;
	use App\Http\Requests;
	use App\Http\Controllers\Controller;
	use Illuminate\Queue\Capsule\Manager;
	use Session;
	use DB;
	use Illuminate\Support\Facades\Input;
	use Illuminate\Support\Facades\Redirect;
	use Illuminate\Support\Facades\Log;
	use Illuminate\Support\Facades\Auth;
	use App\CurrentDay;
	use App\BankShop;
	use App\UserCoupon;

	class CurrentDayController extends Controller
	{
		public function __construct()
		{
			header("Access-Control-Allow-Origin:*"); //*号表示所有域名都可以访问
		}

		// 获取当前地址所在的城市、天气信息
		public function getWeatherInfo(Request $request){
			$city=$this->getCity($request);
//			dd($city);
			$host = "http://jisutqybmf.market.alicloudapi.com";
			$path = "/weather/query";
			$method = "GET";
			$appcode = "1215c3a301254ee79ca773ce9054f2ca";//阿里云appcode
			$headers = array();
			array_push($headers, "Authorization:APPCODE " . $appcode);
			$querys = "city=$city&citycode=citycode&cityid=cityid&ip=ip&location=location";
			$bodys = "";
			$url = $host . $path . "?" . $querys;

			$curl = curl_init();
			curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
			curl_setopt($curl, CURLOPT_URL, $url);
			curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
			curl_setopt($curl, CURLOPT_FAILONERROR, false);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl, CURLOPT_HEADER, false);
			if (1 == strpos("$".$host, "https://"))
			{
				curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
			}
			$result=curl_exec($curl);
			$data=json_decode($result,true);
//			dd($data);
			if($data){
				$dateArr = $data['result'];
					$dates['city'] = $dateArr['city'];
					$dates['weather'] = $dateArr['weather'];
					$dates['templow'] = $dateArr['templow'];
					$dates['temphigh'] = $dateArr['temphigh'];
					$dates['temp'] = $dateArr['temp'];
			}
//			dd($dates);



			return json_encode($dates);
		}

		private function getCity(Request $request){
			$ip = $request->getClientIp();
			$content = file_get_contents("http://api.map.baidu.com/location/ip?ak=o5kQ9hyNX080TUhUGVW3GTyohTwbQyIA&ip=$ip&coor=bd09ll");
			$json = json_decode($content,true);
			if($json['status']==2){
				$city='西安';
			}else{
				$address=$json['address'];
				$cityarr=explode("|", $address);
				$city=$cityarr['2'];//不带"市",如"西安",而不是"西安市"
			}
			return $city;
		}




		// 获取所有网点名称及经纬度
		public function getBankShopNames(){
			$data = array();
			$bankShopArr = CurrentDay::getBankShopLat();
			foreach ($bankShopArr as $key=>$value){
				$data['geo_coord_map'][$key] = "'$value->shopName':[$value->longitude,$value->latitude]" ;
				$data['point_value'][$key] = "{name:'$value->shopName',value:''}";
				$data['line_value'][$key] = array(['name'=>'西安'],['name'=>$value->shopName,'value'=>'']);
			}
			$geo_coord_str = implode(',',$data['geo_coord_map']);
			$data['geo_coord_map']= "{'西安': [108.906690,34.239890],".$geo_coord_str."}" ;
//			dd($data);
			return json_encode($data);
		}


		// 获取当前使用的网点数、名称、下单量、收入金额、 网站top5排名
		public function getBankShopOrderToday(Request $request){
			$data = array();
			$bankShopCount = BankShop::where('state','!=',-1)->count();
			$limit = isset($request->top5) ? 5:$bankShopCount;

			$bankOrderTotal = CurrentDay::getEveryBankShopOrder($limit);
//		dd($bankOrderTotal);
			foreach ($bankOrderTotal as $key=>$orderT){
				$data[$key]['id'] =$orderT->id;
				$data[$key]['shopName'] =$orderT->shopName;
				$data[$key]['state'] =$orderT->state;
				$data[$key]['order_total'] =isset($orderT->order_total) ? $orderT->order_total :0;
				$data[$key]['money_total'] =isset($orderT->money_total) ? $orderT->money_total: 0;
			}

//		dd($data);
			return json_encode($data);
		}

		// 获取注册用户总数
		public function getRegisterToday(Request $request){
			$data['register_total'] = DB::table('user_account')->count();
			return json_encode($data);
		}
		// 成交订单总数
		public function getOrderToday() {
			// 订单量
			$data['order_total'] = DB::table('order')->where('state','>',1)->where('state','<=',4)->count();
			return json_encode($data);
		}

		// 今日收益额、昨日收益、订单总数、折线图
		public function getOrderMoneyToday(Request $request){
			$day= isset($request->day) ? $request->day:'today';
			$orderMoneyArr = CurrentDay::getCurrentDayMoney($day); //今日、昨日、总额
			$data['money_total'] = $orderMoneyArr['money_total'];
			$data['money_today'] = $orderMoneyArr['money_today'];
			$data['money_yestoday'] = $orderMoneyArr['money_yestoday'];

			$data['order_total'] = DB::table('order')->where('state','>',1)->where('state','<=',4)->count();//成交订单总数

			// 折线图数据：开始、结束 日期数组---------------------------------
			$dataArr = array(); // 日期数组
			$moneyArr = array();   // 金额数组
			$orderMoneyTotal  = CurrentDay::getOrderMoneyDay();
			$current_hour = array();
			for ($i=0;$i<24;$i++){
				if($i==0){
					$current_hour[$i] = $i;
				}else{
					$current_hour[$i] = $current_hour[$i-1] + 1;
				}
			}
//			dd($current_hour);
			if($orderMoneyTotal){
				$moneyTotalArr = array(); // 折线图数据：获取数
				foreach($orderMoneyTotal as $key=>$value){
					// 拼接获取数、消耗数 相关数组
					$moneyTotalArr[$key]['times'] = isset($value->times) ? $value->times : 0;   // 时间
					$moneyTotalArr[$key]['xdTotalMoney'] = isset($value->xdTotalMoney) ? $value->xdTotalMoney : 0;   // 下单总金额
				}
				// 组装数据
				foreach ($current_hour as $key=>$value){
					// 日期数组
					$dataArr[] = $value.':00';

					// 下单量数组
					for($i=0;$i<count($moneyTotalArr);$i++){
						if($value == $moneyTotalArr[$i]['times'] ){
							$moneyArr[$key] = (float)$moneyTotalArr[$i]['xdTotalMoney'];
							break;
						}else{
							$moneyArr[$key]= 0;
						}
					}
				}
			}else{
				foreach ($current_hour as $key=>$value){
					// 日期数组
					$dataArr[] = $value.':00';
					$moneyArr[] = 0;
				}
			}

//
//		echo '<pre>';
//		print_r($moneyArr);
//		print_r($dataArr);
//		exit;

			$dataArr = json_encode($dataArr);
			$moneyArr = json_encode($moneyArr);
			$data['dataArr'] = $dataArr;
			$data['moneyArr'] = $moneyArr;

			return json_encode($data);
		}


		/*// 今日订单交易详情
		public function getOrderInfoToday(Request $request){
			$num = 20;
			$cpage= isset($request->page) ? $request->page:1;//当前页
//		return $cpage;
//		dd($request->page);
			$total = CurrentDay::getCurrentDayTotal();
			$pagenum=ceil($total/$num);//总页数

			$offset=($cpage-1)*$num;//开始去数据的位置
			$data['pageTotal'] = $pagenum;
			$data['infoList']= CurrentDay::getCurrentDayOrderInfo($offset,$num);
//		dd($data);
			return json_encode($data);
		}*/

		public function getOrderInfoToday(Request $request){
			$data['infoList']= CurrentDay::getCurrentDayOrderInfo();
//		dd($data);
			return json_encode($data);
		}


		// 获取折线图数据
		public function getMoneyChartData(){
			$dataArr = array(); // 日期数组
			$moneyArr = array();   // 金额数组

			$orderMoneyTotal  = CurrentDay::getOrderMoneyDay();
			// 折线图数据：开始、结束 日期数组
			$current_hour = array();
			for ($i=0;$i<24;$i++){
				if($i==0){
					$current_hour[$i] = $i;
				}else{
//					$current_hour[$i] = sprintf("%02d", ($current_hour[$i-1] + 1));
					$current_hour[$i] = $current_hour[$i-1] + 1;
				}
			}
//			dd($current_hour);

			if($orderMoneyTotal){
				$moneyTotalArr = array(); // 折线图数据：获取数
				foreach($orderMoneyTotal as $key=>$value){
					// 拼接获取数、消耗数 相关数组
					$moneyTotalArr[$key]['times'] = isset($value->times) ? $value->times : 0;   // 时间
					$moneyTotalArr[$key]['xdTotalMoney'] = isset($value->xdTotalMoney) ? $value->xdTotalMoney : 0;   // 下单总金额
				}
				// 组装数据
				foreach ($current_hour as $key=>$value){
					// 日期数组
					$dataArr[] = $value.':00';

					// 下单量数组
					for($i=0;$i<count($moneyTotalArr);$i++){
						if($value == $moneyTotalArr[$i]['times'] ){
							$moneyArr[$key] = (float)$moneyTotalArr[$i]['xdTotalMoney'];
							break;
						}else{
							$moneyArr[$key]= 0;
						}
					}
				}
			}else{
				foreach ($current_hour as $key=>$value){
					// 日期数组
					$dataArr[] = $value.':00';
					$moneyArr[] = 0;
				}
			}

//
//		echo '<pre>';
//		print_r($moneyArr);
//		print_r($dataArr);
//		exit;

			$dataArr = json_encode($dataArr);
			$moneyArr = json_encode($moneyArr);
			$data['dataArr'] = $dataArr;
			$data['moneyArr'] = $moneyArr;

			return json_encode($data);
		}











		// 获取所有网点优惠券总数、使用数
		public function  getBankShopCouponTotal(){
			$data['couponTotal'] = UserCoupon::count();
			$data['couponNoUse'] = UserCoupon::where('useStatus',1)->count();
			$data['couponUsed'] = UserCoupon::where('useStatus',2)->count();
			$data['couponExpired'] = UserCoupon::where('useStatus',3)->count();
			dd($data);
			return json_encode($data);
		}


		// 获取各个网点优惠券数及使用数
		public function getEveryBankShopCoupon(){

		}



























	}





